package sunshine.sun.cn.notifidemo;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import java.util.ArrayList;
import java.util.List;

public class SoundPoolUtil {
    public static SoundPool mSoundPlayer;
    public static Context mContext;
    private static AudioManager audioManager;
    private static List<String> paths = null;
    private static List<Integer> pathsRaw = null;
    private static boolean isSDVoice = false;
    public interface VOICE {
        int VOICE_Information = 1;
        int VOICE_Question = 2;
        int VOICE_Warning = 3;
        int VOICE_Error = 4;
        int VOICE_Success = 5;
    }
    public static void setIsSDVoice(boolean isSDVoice) {
        SoundPoolUtil.isSDVoice = isSDVoice;
        mSoundPlayer.release();
        initSoundPool(mContext);
    }

    //初始化声音池
    public static void initSoundPool(Context context) {
        mContext = context.getApplicationContext();
        mSoundPlayer = new SoundPool(10, AudioManager.STREAM_MUSIC, 5);
        if (isSDVoice && paths != null && paths.size() > 0) {
            for (int i = 0; i < paths.size(); i++) {
                mSoundPlayer.load(paths.get(i), 1);// 1
            }
//            mSoundPlayer.load(path + "/Voice/" + "Information.wav", 1);// 1
//            mSoundPlayer.load(path + "/Voice/" + "Question.wav", 1);// 2
//            mSoundPlayer.load(path + "/Voice/" + "Warning.wav", 1);// 3
//            mSoundPlayer.load(path + "/Voice/" + "Error.wav", 1);// 4
//            mSoundPlayer.load(path + "/Voice/" + "scanFailure.mp3", 1);// 5
//            mSoundPlayer.load(path + "/Voice/" + "scanSuccess.mp3", 1);// 6
//            mSoundPlayer.load(path + "/Voice/" + "Voice.wav", 1);// 7
        }
        if (!isSDVoice && pathsRaw != null && pathsRaw.size() > 0) {
            for (int i = 0; i < pathsRaw.size(); i++) {
                mSoundPlayer.load(mContext, pathsRaw.get(i), 1);// 1
            }
//            mSoundPlayer.load(mContext, R.raw.information, 1);// 1
//            mSoundPlayer.load(mContext, R.raw.question, 1);// 2
//            mSoundPlayer.load(mContext, R.raw.warning, 1);// 3
//            mSoundPlayer.load(mContext, R.raw.error, 1);// 4
//            mSoundPlayer.load(mContext, R.raw.scanfailure, 1);// 5
//            mSoundPlayer.load(mContext, R.raw.scansuccess, 1);// 6
//            mSoundPlayer.load(mContext, R.raw.voice, 1);// 7
        }
        audioManager = (AudioManager) mContext.getSystemService(mContext.AUDIO_SERVICE);
    }

    /**
     * 添加音频
     * 下表+1即为播放的id
     *
     * @param data
     */
    public static void addSoundsFromSD(List<String> data) {
        if (paths == null) {
            paths = new ArrayList<>();
        }
        paths.addAll(data);
    }

    /**
     * 添加音频
     * 下表+1即为播放的id
     *
     * @param data
     */
    public static void addSoundsFromRaw(List<Integer> data) {
        if (pathsRaw == null) {
            pathsRaw = new ArrayList<>();
        }
        pathsRaw.addAll(data);
    }
    private static long currentTime;
    //播放声音池声音
    public static void play(int sound) { //返回当前AlarmManager最大音量
        if(System.currentTimeMillis()-currentTime<200){
            return;
        }
        float audioCurrentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        mSoundPlayer.play(sound, 1, 1, 0, 0, 1);
        currentTime=System.currentTimeMillis();
    }
}
